<?php

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('htfrmmanagerhotel_loadMenu');      
$objXajax->registerFunction('htfrmmanagerhotel_loadGrid');      
$objXajax->registerFunction('htfrmmanagerhotel_checkedRoom');
$objXajax->registerFunction('htfrmmanagerhotel_Init');
$objXajax->registerFunction('htfrmmanagerhotel_addRemoveProduct');
$objXajax->registerFunction('htfrmmanagerhotel_moveRoom');
$objXajax->registerFunction('htfrmmanagerhotel_loadProduct');
$objXajax->registerFunction('htfrmmanagerhotel_finishRoom');
$objXajax->registerFunction('htfrmmanagerhotel_moveRoomNew');
$objXajax->registerFunction('htfrmmanagerhotel_addNewProduct');
$objXajax->registerFunction('htfrmmanagerhotel_saveCustomer');
$objXajax->registerFunction('htfrmmanagerhotel_deleteCustomer');
$objXajax->registerFunction('htfrmmanagerhotel_ajxTotalRoom');
/*
* hàm dùng để khởi tạo cho các biến về giá phòng và giá các sản phẩm
* 
*/
$roomPriceTotal=0;
function  htfrmmanagerhotel_Init(){
    global $objDbSelect,$lang;
    $hotel_id = $_SESSION['current']['hotel_id'];
    $objResponse = new xajaxResponse();
    $sqlroomtype = "select * from tbl_room_type where hotel_id = '$hotel_id'";
    $arrRoomType = $objDbSelect->GetArray($sqlroomtype); 
    $objResponse = new xajaxResponse();
    for($i=0;$i<count($arrRoomType);$i++){
       $id=$arrRoomType[$i]['id'] ;
       $name=$arrRoomType[$i]['name'] ;
       $color=$arrRoomType[$i]['color'] ;
       $priceone=$arrRoomType[$i]['priceone'] ;
       $pricetwo=$arrRoomType[$i]['pricetwo'] ;
       $pricethree=$arrRoomType[$i]['pricethree'] ;
       $pricefour=$arrRoomType[$i]['pricefour'] ;
       $pricefive=$arrRoomType[$i]['pricefive'] ;
       $priceinday=$arrRoomType[$i]['priceinday'] ;
       $objResponse->addScript("ht_obj.createAssignRoomPrice('$id','$name','$priceone','$pricetwo','$pricethree','$pricefour','$pricefive','$priceinday','$color');"); 
    }
    $sqlconfig = "Select * from tbl_config where hotel_id = '$hotel_id' "; 
    $arrConfig = $objDbSelect->GetArray($sqlconfig);
    if(is_array($arrConfig)&& count($arrConfig)>0){
        $saturdayadd = $arrConfig[0]['fee_saturday'];
        $sundayadd = $arrConfig[0]['fee_sunday'];
        $starttime = $arrConfig[0]['starttime'];
        $endtime = $arrConfig[0]['endtime'];
        $objResponse->addScript("ht_obj.setaddDate($saturdayadd,$sundayadd);"); 
        $objResponse->addScript("ht_obj.setTimeRoom($starttime,$endtime);"); 
    }
    $sqlProduct = "Select a.name_$lang as name,a.id,a.unit,a.unitname,a.price  from tbl_product_new a join tbl_product_new b on a.parent_id = b.id where a.active = 1 and b.active = 1 and a.product_type=1 and b.hotel_id = '$hotel_id'"; 
                // echo $sqlProduct; die();
    $arrProduct = $objDbSelect->GetArray($sqlProduct);
    $count = count($arrProduct);
    for($i=0;$i<$count;$i++){
        $name = $arrProduct[$i]['name'];
        $price = $arrProduct[$i]['price'];
        $id = $arrProduct[$i]['id'];
        $unit = $arrProduct[$i]['unit'];
        $unitname = $arrProduct[$i]['unitname'];
        $objResponse->addScript("ht_obj.createAssignProductPrice($id,'$name','$price','$unit','$unitname');"); 
    }
    $indexNewProduct = time();
    $objResponse->addScript("ht_obj.indexNewProduct=$indexNewProduct;");
    return $objResponse->getXML();
    
}
/*function  htfrmmanagerhotel_checkedRoom($room_id) {// kiem tra xem phong do da dat chua?
    global $objDbSelect;
    $user_id = $_SESSION['current']['user_id'];
    $hotel_id = $_SESSION['current']['hotel_id'];
    $objResponse = new xajaxResponse();
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $sql =  "select UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(timebegin) as timestamp,id from tbl_current_book_room$tbl_postfix 
    where room_id = '$room_id'";
    $arr = $objDbSelect->GetArray($sql);
    //echo $sql; die();
    if(count($arr)>0 && is_array($arr)){
        $time = $arr[0]['timestamp'];
        $id = $arr[0]['id'];
        $objResponse->addAlert('Phòng đang hoạt động!');
       $objResponse->addScript( "ht_obj.createAssignStatus($room_id,$time,1,$id);");           
    }else{
       $sql = "insert into tbl_current_book_room$tbl_postfix(room_id,timebegin,user_created,hotel_id) value($room_id,now(),'$user_id','$hotel_id')";
       $objDbSelect->Execute($sql);
       $id = mysql_insert_id();
       $objResponse->addScript( "ht_obj.createAssignStatus($room_id,0,1,'$id');");                                                       
    }
    return $objResponse->getXML();
}*/    
 //  global $arrMenu;
function htfrmmanagerhotel_save($params)
{
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $hotel_id = $_SESSION['current']['hotel_id'];   
    $user_id = $_SESSION['current']['user_id'];        
    $name_vi = $params['txtnamemenuVi'];
    $name_en = $params['txtnamemenuEn'];
    $parent_id = $params['cbbMenuParent'];
    $price = $params['txtpricemenu'];
    $unit = $params['txtunitmenu'];
    $unitname = $params['txtnameunit'];
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql .="UPDATE tbl_product_new SET name_vi = '$name_vi',name_en = '$name_en',parent_id = '$parent_id',
            price = '$price',unit = '$unit',nameunit='$unitname',user_update = '$user_id',date_update = now() WHERE id =$id"; 
        $objDbSelect->Execute($sql);
        
    }else{
          $sql.= " INSERT INTO tbl_product_new(name_vi,name_en,active,unit,nameunit,price,parent_id,user_created,
          date_created,user_update,date_update) VALUES('$name_vi','$name_en',1,'$unit',
          '$unitname','$price','$parent_id','$user_id',now(),'$user_id',now())"; 
          $objDbSelect->Execute($sql); 
     }    
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}

function htfrmmanagerhotel_loadMenu(){     

    global $lang,$arrMenu;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $lang ='vi';
    $strMenu = htfrmmanagerhotel_getMenu();    
    $objResponse->addAssign('htfrmmanagerhotel_leftmenu', 'innerHTML', $strMenu);
    $objResponse->addScriptCall("htfrmmanagerhotel_dropdrapmenu();");  
    $objResponse->addScriptCall("addJsContext();");                              
                                 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function htfrmmanagerhotel_loadGrid(){     

    global $lang,$arrMenu,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $lang ='vi';
    $arrRoom = htfrmmanagerhotel_getRoom();
   // $objResponse->addScript("var ht_obj = new classRoom();"); 
    for($i=0;$i<count($arrRoom);$i++){
       $room_id = $arrRoom[$i]['id'];
       $name = $arrRoom[$i]['name'];
       $room_type = $arrRoom[$i]['room_type'];
       $timestamp = $arrRoom[$i]['timestamp'];
       $status =  $arrRoom[$i]['timestamp']>0?1:0;           
       $current_id =  $arrRoom[$i]['current_id'];
       $objResponse->addScript("ht_obj.createAssignRoom('$room_id','$name','$room_type','$timestamp','$status','$current_id');");
      // $sql = "select "; 
                                         //function(id,name,roomtype,timestamp,status,current_id){
    }
    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_managerhotel_grid.html');
    $oTbs->MergeBlock('blk_data', $arrRoom);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('htfrmmanagerhotel_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("addJsContext();");  
    $objResponse->addScript("ht_obj.initSetCustomer();"); 
    $arrCustomer = htfrmmanagerhotel_getCustomer(); 
    for($i=0;$i<count($arrCustomer);$i++){
        $name = $arrCustomer[$i]['name'];
        $cmnd = $arrCustomer[$i]['cmnd'];
        $age = $arrCustomer[$i]['age'];
        $sex = $arrCustomer[$i]['sex'];
        $moto = $arrCustomer[$i]['moto'];
        $address = $arrCustomer[$i]['address'];
        $generate_id = $arrCustomer[$i]['generate_id'];
        $room_id = $arrCustomer[$i]['room_id'];
        $dum_id = $arrCustomer[$i]['dum_id'];
        $objResponse->addScript("ht_obj.assignOldCustomer('$room_id','$dum_id','$name','$cmnd','$age','$address','$sex','$moto');");  
    }
            
   $objResponse->addScript('$(".clsbtn").button();');          
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  htfrmmanagerhotel_getRoom(){
global $objDbSelect;
$hotel_id = $_SESSION['current']['hotel_id'];
$tbl_postfix = $_SESSION['current']['tbl_postfix'];
$sqlSelect = "SELECT br.id as current_id,r.id,r.room_type,r.name,t.color, if(br.timebegin is null,0,
(UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(br.timebegin))) as `timestamp`
 FROM tbl_room as r join tbl_room_type  as t on r.room_type=t.id left 
 join tbl_current_book_room$tbl_postfix br  on br.room_id = r.id  where t.hotel_id='$hotel_id' order by r.room_type,r.name";
 //echo $sqlSelect; die();
return $objDbSelect->GetArray($sqlSelect);
}
function htfrmmanagerhotel_getCustomer(){
 global $objDbSelect;
$hotel_id = $_SESSION['current']['hotel_id'];
$tbl_postfix = $_SESSION['current']['tbl_postfix'];
$sqlSelect=" SELECT br.room_id,br.id,c.*  FROM tbl_room as r join tbl_room_type  as t on r.room_type=t.id left 
 join tbl_current_book_room$tbl_postfix br  on br.room_id = r.id join tbl_customer$tbl_postfix c 
 on br.id= c.generate_id where t.hotel_id='$hotel_id' order by c.dum_id" ; 
// echo $sqlSelect; die();
return $objDbSelect->GetArray($sqlSelect); 
}

function htfrmmanagerhotel_getMenu(){
    global $objDbSelect,$lang;
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sql = "select p1.name_$lang as name,p1.price as price,p1.id,p1.product_type ,p2.name_$lang as parentname ,p2.icon
        from tbl_product_new as p1 join tbl_product_new as p2 on p1.parent_id
    = p2.id and p2.active = 1 where p2.hotel_id = '$hotel_id' and p1.active = 1 order by p1.parent_id,p1.product_type " ;
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $arrResult = array();
    $index =-1;  
    $strresult='';
    $first = true;

    for($i=0;$i<$count;$i++){
        $name = $arr[$i]['name'];
        $price = $arr[$i]['price'];
        if($arr[$i]['product_type']==0){
            if(!$first){
             $strresult .= "</div>" ;             
            } 
            $strresult.="<div><span>$name</span>" ; 
            $first = false;
        }else{
            $id = $arr[$i]['id'];
           $strresult.="<a href='#' id='htfrmmanagerhotel_liProd_$id'>$name</a>" ;         
        }
    }
    
    $strresult = '<div style="float: left" id="my_menu" class="sdmenu">'.$strresult."</div></div>" ;
    return $strresult;
}
                                           
function htfrmmanagerhotel_addRemoveProduct($arrAddRemove,$arrProduct=''){
     global $lang,$arrMenu,$objDbSelect;
     $objResponse = new xajaxResponse();
     $arrId= explode(';',$i);
     for($i=0;$i<count($arrAddRemove);$i++){
         $temp = $arrAddRemove[$i];
         $arrId = explode(';',$temp);
         $generate_id = $arrId[1];
         $product_id = $arrId[0];
         $nump = $arrId[2];
         htfrmmanagerhotel_actionDB($generate_id,$product_id,$nump,$arrProduct) ;
     }    
    
     return $objResponse->getXML();    
}

function htfrmmanagerhotel_actionDB($generate_id,$product_id,$nump,$arrProduct){
  global $objDbSelect;
       $user_id = $_SESSION['current']['user_id'];
       $hotel_id = $_SESSION['current']['hotel_id'];
     $tbl_postfix = $_SESSION['current']['tbl_postfix'];
       $sqlSelect = "select * from tbl_history_product$tbl_postfix where generate_id =  
     '$generate_id' and product_id = '$product_id'";
     $arrSelect = $objDbSelect->GetArray($sqlSelect);
     $sql ='';
     if(is_array($arrSelect) && count($arrSelect)==1){
         $curnum =  $arrSelect[0]['num'];// sớ sản phẩm đã có hiện tại
         $nextnum = $curnum+ $nump;
         if($nextnum<1){
             $sql= "delete from tbl_history_product$tbl_postfix where generate_id =  
     '$generate_id' and product_id = '$product_id'";     
     // write log........................
         }else{
            $sql = "update tbl_history_product$tbl_postfix SET num = '$nextnum' where generate_id =  
     '$generate_id' and product_id = '$product_id'";
      // write log........................ 
         }
     }else{
         $name = $arrProduct[$product_id]['name'];
         $price = $arrProduct[$product_id]['price'];
       $sql = "insert into tbl_history_product$tbl_postfix(generate_id,product_id,name,price,date_created,user_created,num,hotel_id) 
       values('$generate_id','$product_id','$name','$price',now(),'$user_id',$nump,'$hotel_id')"; 
       // write log........................   
     }      
     $objDbSelect->Execute($sql);  
}

function htfrmmanagerhotel_moveRoom($arrMove){
   global $objDbSelect;
   for($i=0;$i<count($arrMove);$i++){
       $temp  = $arrMove[$i]; 
       $arrId = explode(';',$temp);
       $preRoom = $arrId[0];
       $curRoom = $arrId[1];
       $sql = "update tbl_current_book_room set room_id = $curRoom , flg_move = 1 where room_id = $preRoom";
       $objDbSelect->Execute($sql); 
   }
   // write log........................
   $objResponse = new xajaxResponse(); 
   return $objResponse->getXML();    
}

function htfrmmanagerhotel_moveRoomNew($roomFrom,$roomTo){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $sql = "update tbl_current_book_room$tbl_postfix set room_id = $roomTo,flg_move = 1 where room_id = $roomFrom";
    $objDbSelect->Execute($sql); 
    return $objResponse->getXML();  
}

function htfrmmanagerhotel_loadProduct(){
    global $objDbSelect;
     $objResponse = new xajaxResponse();
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sql = "select * from tbl_current_book_room$tbl_postfix where room_id in 
            (select id from tbl_room where room_type in (select id from tbl_room_type where hotel_id = '$hotel_id'))";
    $arr = $objDbSelect->GetArray($sql);
    for($i=0;$i<count($arr);$i++){
        $room_id = $arr[$i]['room_id'];
        $generate_id = $arr[$i]['id'];
        $sqltemp = "select * from tbl_history_product$tbl_postfix where generate_id = '$generate_id' order by date_created";
        $arrProduct =  $objDbSelect->GetArray($sqltemp);
        for($j=0;$j<count($arrProduct);$j++){
            $name = $arrProduct[$j]['name'];
            $num = $arrProduct[$j]['num'];
            $product_id= $arrProduct[$j]['product_id'];
            $objResponse->addScript("frmLoadProduct('$room_id','$generate_id','$product_id','$name','$num');");
       }
    }
   return $objResponse->getXML();  
}

function htfrmmanagerhotel_finishRoom($room_id,$roomPrice,$totalPrice){
    
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $user_id = $_SESSION['current']['user_id'];
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $sql = "insert into tbl_history_book_room$tbl_postfix(room_id,generate_id,timebegin,timeend,user_update,user_created,price,totalprice,flg_move,hotel_id) 
    select '$room_id',id,timebegin,now(),'$user_id',user_created,$roomPrice,'$totalPrice',flg_move,hotel_id from tbl_current_book_room$tbl_postfix where room_id = '$room_id'";
    $objDbSelect->Execute($sql);    
$sqldelete = "delete from tbl_current_book_room$tbl_postfix where room_id = '$room_id'";
    $objDbSelect->Execute($sqldelete);
    return $objResponse->getXML();
}
function htfrmmanagerhotel_addNewProduct($proId,$current_id,$name,$price){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $user_id = $_SESSION['current']['user_id'];
    $hotel_id = $_SESSION['current']['hotel_id'];
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $sql =''; 
    $sql = "insert into tbl_history_product$tbl_postfix(generate_id,product_id,name,price,date_created,user_created,num,pro_type,hotel_id) 
    values('$current_id','$proId','$name','$price',now(),'$user_id',1,1,'$hotel_id')"; 
       // write log........................   
     $objDbSelect->Execute($sql);  
    return $objResponse->getXML();    
}

function htfrmmanagerhotel_saveCustomer($current_id,$name,$cmnd,$age,$address,$sex,$moto,$indexNewProduct,$flg=0){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $user_id = $_SESSION['current']['user_id'];
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $name = addslashes($name);
    $address = addslashes($address); 
    $sql ='';
    if($flg==0){
        $sql = " insert into tbl_customer$tbl_postfix(generate_id,name,cmnd,sex,address,age,moto,dum_id)
         value('$current_id','$name','$cmnd','$sex','$address','$age','$moto','$indexNewProduct') " ;
    }else{
       $sql = "UPDATE tbl_customer$tbl_postfix SET name='$name',cmnd='$cmnd',age='$age',sex='$sex',address='$address',moto='$moto'
       WHERE generate_id ='$current_id' and dum_id='$indexNewProduct'"; 
    }
    $objDbSelect->Execute($sql);
   // echo $sql;die();
    return $objResponse->getXML();    
}

function htfrmmanagerhotel_deleteCustomer($generate_id,$dum_id){
    global $objDbSelect;
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $sql = "delete from tbl_customer$tbl_postfix where generate_id = '$generate_id' and dum_id ='$dum_id' ";
    $objDbSelect->Execute($sql);
    $objResponse = new xajaxResponse();
    return $objResponse->getXML();    
}

/*
* Load lại thông tin các dịch vụ mà mỗi lần KH cập nhật 
* param1  $id_room : phòng mà KH đang ở.
* param2  $viewOrder : có giá trị 1(load khi thêm mới),2(load khi tính tien),3(load lại khi cập nhật)   ,4 (load lại khi xóa dịch vụ)   
*/
function htfrmmanagerhotel_ajxTotalRoom($id_room,$arrRoomPrice,$startTime,$endTime,$generate_id,$flgCal)
{
  global $objDbSelect,$displayOrder,$displayTitle,$lang,$roomPriceTotal;
  $tbl_postfix = $_SESSION['current']['tbl_postfix'];
  $objResponse = new xajaxResponse();
  try{
      $objResponse = new xajaxResponse();
      $sqlS =  "SELECT id FROM tbl_current_book_room$tbl_postfix WHERE room_id = '$id_room'";
      $rs = $objDbSelect->Execute($sqlS);
      if (!$rs || $rs->RecordCount() == 0)
      {
           $objResponse->addScript("window.location.reload();"); 
            return $objResponse -> getXML();  
      }
      
      $sqlS = " SELECT c.id,c.timebegin as timeStart,now() as timeEnd,(UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(c.timebegin))/3600 as timeTotal FROM tbl_current_book_room$tbl_postfix c  WHERE c.id = '$generate_id' ";               
      $rs = $objDbSelect->GetArray($sqlS);
      
      $arrayTemp = array();
      $roomPriceTotal = htfrmmanagerhotel_calRentRoom($rs,$arrRoomPrice,$startTime,$endTime,$flgCal);
     //echo $flgCal; die();
      $SQL = "SELECT name as name,price as price,num as num,price*num as total FROM tbl_history_product$tbl_postfix  WHERE generate_id = '$generate_id' ";      
        $oTbs = new clsTinyButStrong();   
        $oTbs ->LoadTemplate("../templates/$lang/$path/main_managerhotel_orderinfo_grid_2.html") ;
        $oTbs->Show(TBS_NOTHING); 
        $oTbs->MergeBlock('blk_data', $objDbSelect->_connectionID, $SQL);           
        $objResponse->addAssign('htfrmmanagerhotel_billOrder', 'innerHTML', $oTbs->Source);
        $objResponse->addScript("htfrmmanagerhotel_totalAll();");
       // $objResponse->addScript("$("htfrmmanagerhotel_billOrder").dialog('option', 'title', title); ");
        $objResponse->addScript("$('#htfrmmanagerhotel_billOrder').dialog('open');");
        
  } 
  catch (Exception $e) {
         // $objResponse->addAlert("Lỗi :".$e->getMessage());
  }      
 return $objResponse -> getXML();                 
}

function htfrmmanagerhotel_calRentRoom($arrCurrent,$arrRoomPrice,$startTime,$endTime,$flgCal){
    
    $timeStart = $arrCurrent[0]['timeStart']; 
    $timeEnd   = $arrCurrent[0]['timeEnd'];
    $timeTotal = $arrCurrent[0]['timeTotal'];
    $maxTimeRange = 5;   // khoảng tối đa thuê theo giờ.
    $moneyperday = $arrRoomPrice['priceDay'];   // số tiền trả trên 1 ngày thuê phòng.
    
    
    $hourStart = substr($timeStart,11,2);
    $hourStart =  (int)$hourStart;
    $hourEnd   =  substr($timeEnd,11,2);
    $hourEnd =  (int)$hourEnd+1;       
    $dayRent = (int)($timeTotal/24) ;
    $hourRent =  $timeTotal%24;  
    $moneyResult = 0;  
   if($flgCal==1){
        if($timeTotal<=$maxTimeRange){
            $timeTotalIndex =  ceil($timeTotal);
             
            $moneyResult = $arrRoomPrice['price'.$timeTotalIndex] ; // tạo ra cái mãng. ứng với từ mấy giờ đến mấy giờ thì giá tiền là bao nhiêu.
        }else{       

        if(($hourStart>=$startTime&&$hourStart<=$endTime)||($hourEnd>=$startTime&&$hourEnd<=$endTime)){
            // trường hợp giờ thuê hoặc giờ trả phòng rơi vào khoảng thời gian checkout

            if($hourRent<=$maxTimeRange){ // trường hợp giờ thuê bị vơi ra nhỏ hơn trong khoảng thuê theo giờ
                $timeTotalIndex =  ceil($hourRent);
                $moneyResult = $arrRoomPrice['price'.$timeTotalIndex] + $dayRent*$moneyperday ;
            }else{
                $moneyResult =  ($dayRent+1)*$moneyperday ; 
            }      
        }else{ // trường hợp giờ thuê hoặc giờ trả phòng không rơi vào khoảng thời gian checkout 
            // if($dayRent==0){// trường hợp thuê dưới 24h
            if(htfrmmanagerhotel_checkSpendTimeOut($hourStart,$hourEnd,$endTime)){
                $timeMin = htfrmmanagerhotel_getMinTime($hourStart,$hourEnd,$startTime,$endTime) ;
                if($timeMin>$maxTimeRange){
                $moneyResult =  $moneyperday*2;
                }else{
                $moneyResult = $arrRoomPrice['price'.$timeMin] + $moneyperday ;  
                }
            }else{
                if($hourEnd-$endTime<=$maxTimeRange){
                $timeMin     = $hourEnd-$endTime;
                $moneyResult = $arrRoomPrice['price'.$timeMin];
                }else{
                $moneyResult = $moneyperday;   
                }

            }
            $moneyResult += $dayRent*$moneyperday ;
            
            }

        }
       
   }else{
      if($hourRent<$maxTimeRange){
          $timeMin = $hourRent+1;  
          $moneyResult = $arrRoomPrice['price'.$timeMin];
      }else{
         $moneyResult = $moneyperday; 
      }
      $moneyResult += $dayRent*$moneyperday ;   
   }
   return $moneyResult; 
    
}

function htfrmmanagerhotel_checkSpendTimeOut($timeStart,$timeEnd,$timeOut){
     
    if($timeStart<$timeOut && ($timeEnd>$timeOut||$timeStart>$timeEnd)|| $timeStart>$timeOut && $timeEnd>$timeOut && $timeEnd<$timestart){
         return true;
     }
     return false;
}
// lấy khoảng thời gian nhỏ nhất từ khoảng thời gian cho phép đến  thời gian 
function htfrmmanagerhotel_getMinTime($timestart,$timeEnd,$timeStartNorm,$timeEndNorm){
      $timeTemp1 = $timeStartNorm  -  $timestart;
      if($timeTemp1<0)$timeTemp1= 24 + $timeTemp1;
      $timeTemp2 = $timeEnd - $timeEndNorm;
      if($timeTemp2<0)$timeTemp2 = 24 + $timeTemp2;
      if($timeTemp1>$timeTemp2){
         return $timeTemp2; 
      }      
      return $timeTemp1;      
}


 
    
?>
